package com.easy.common.util;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;


public class I18n {
	private static final String DEFAULT_I18_RESOURCES = "com.easy.lang.Lang";
	private ServletContext servletContext;
	private HttpServletRequest request;
	private Class<?> jspClass;
	private ResourceBundle resources;

	public boolean isChineseContext()
	{
		return lm("common.insert.ok").indexOf("successfully") < 0;
	}

	public I18n()
	{
		this(DEFAULT_I18_RESOURCES, Locale.getDefault());
	}

	public I18n(String resrcName)
	{
		this(resrcName, Locale.getDefault());
	}

	public I18n(Locale currentLocale)
	{
		this(DEFAULT_I18_RESOURCES, currentLocale);
	}

	public I18n(String resrcName, String language, String country)
	{
		this(resrcName, new Locale(language, country));
	}

	public I18n(String resrcName, Locale currentLocale)
	{
		servletContext = null;
		request = null;
		jspClass = null;
		resources = null;
		resources = ResourceBundle.getBundle(resrcName, currentLocale);
	}

	public I18n(ServletContext servletContext, HttpServletRequest request, Class<?> jspClass)
	{
		this.servletContext = null;
		this.request = null;
		this.jspClass = null;
		resources = null;
		this.servletContext = servletContext;
		this.request = request;
		this.jspClass = jspClass;
	}

	public static String ln(Class<?> cl, String postFix)
	{
		return (new StringBuilder(String.valueOf(cl.getName()))).append("_").append(postFix).toString();
	}

	private static String trnsNameJsp(String jspName)
	{
		StringBuffer rtBuff = new StringBuffer();
		rtBuff.append(jspName.substring("org.apache.".length(), jspName.length() - 4));
		rtBuff.append(".jsp");
		return rtBuff.toString();
	}

	public String lnj(String postFix)
	{
		StringBuffer rtBuff = new StringBuffer();
		rtBuff.append((new StringBuilder(String.valueOf(trnsNameJsp(jspClass.getName())))).append("_").append(postFix).toString());
		return rtBuff.toString();
	}

	public String lmd(Class<?> cl, String postFix)
	{
		return lm(ln(cl, postFix));
	}

	public String ls(String msrg, String valueStr)
	{
		return ls(null, msrg, new String[] {
			valueStr
		});
	}

	public String ls(String msrg, String valueArray[])
	{
		return ls(null, msrg, valueArray);
	}

	public String ls(String rscId, String msrg, String valueArray[])
	{
		if (msrg == null)
			return "";
		String rtStr = null;
		if (resources == null)
			rtStr = ServletUtility.getLocaleMsrg(servletContext, request, rscId, msrg);
		else
			rtStr = resources.getString(msrg);
		if (rtStr == null)
			rtStr = msrg;
		return RegexpUtility.assignVar(rtStr, valueArray);
	}
	/**
	 * 
	 * @param msrg
	 * @return
	 */
	public String getText(String msrg)
	{
		try{
			return lm(msrg);
		}catch(Exception ex){return "";}
	}
	/**
	 * 
	 * @param msrg
	 * @param valuearray
	 * @return
	 */
	public String getText(String msrg,String valuearray[])
	{
		try{
			return ls(msrg,valuearray);
		}catch(Exception ex){return "";}
	}
	public String lm(String msrg)
	{
		return lm(null, msrg);
	}

	public String lm(String rscId, String msrg)
	{
		if (msrg == null)
			return "";
		String msrgArray[] = msrg.split((new StringBuilder("\\")).append(String.valueOf('$')).toString());
		StringBuffer rtBuff = new StringBuffer();
		for (int i = 0; i < msrgArray.length; i++)
		{
			String rtStr = null;
			if (resources == null)
				rtStr = ServletUtility.getLocaleMsrg(servletContext, request, rscId, msrgArray[i]);
			else
				rtStr = resources.getString(msrg);
			if (rtStr != null)
				rtBuff.append(rtStr);
		}

		return rtBuff.toString().trim();
	}

	public String lmj(String msrg)
	{
		return lmj(null, msrg);
	}

	public String lmj(String rscId, String msrg)
	{
		StringBuffer rtBuff = new StringBuffer();
		rtBuff.append(ServletUtility.getLocaleMsrg(servletContext, request, rscId, lnj(msrg)));
		return rtBuff.toString();
	}

	public String lmk(String msrg)
	{
		return lmk(null, msrg);
	}

	public String lmk(String rscId, String msrg)
	{
		StringBuffer rtBuff = new StringBuffer();
		rtBuff.append(ServletUtility.getLocaleMsrg(servletContext, request, rscId, msrg));
		return rtBuff.toString();
	}
}
